{include file="base/_meta" /}
<style>
    #authoritiesTable + .layui-table-view .layui-table-body tbody > tr > td {
        padding: 0;
    }

    #authoritiesTable + .layui-table-view .layui-table-body tbody > tr > td > .layui-table-cell {
        height: 48px;
        line-height: 48px;
    }

    .tb-img-circle {
        width: 40px;
        height: 40px;
        cursor: zoom-in;
        border-radius: 50%;
        border: 2px solid #dddddd;
    }
</style>
</head>
<body>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">分类名称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入分类名称"/>
                        </div>
                    </div>
                    <div class="layui-inline layui-btn-group">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="authoritiesTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>&nbsp;
                        <button class="layui-btn icon-btn" onclick="window.location.reload();">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>&nbsp;
                        <button id="authoritiesAddBtn" class="layui-btn icon-btn" type="button">
                            <i class="layui-icon">&#xe654;</i>添加
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="authoritiesTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="authoritiesTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="authoritiesEditDialog">
    <form id="authoritiesEditForm" lay-filter="authoritiesEditForm" class="layui-form model-form"
          style="padding-right: 20px;">
        <input type="hidden" name="__token__" value="{$Request.token}" />
        <input name="id" type="hidden"/>

        <div class="layui-row layui-col-space25">
            <div class="layui-col-sm6">
                <div class="layui-form-item">
                    <label class="layui-form-label">上级菜单</label>
                    <div class="layui-input-block">
                        <div id="authoritiesEditParentSel" class="ew-xmselect-tree"></div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label layui-form-required">分类名称:</label>
                    <div class="layui-input-block">
                        <input name="name" placeholder="请输入分类名称" class="layui-input"
                               lay-verType="tips" lay-verify="required" required autocomplete="off"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="show" value="10" title="显示">
                        <input type="radio" name="show" value="20" title="隐藏">
                    </div>
                </div>
            </div>
            <div class="layui-col-sm6">
                <div class="layui-upload-drag" id="site_logo" onclick="upload_file('site_logo',1)">
                    <i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>
                    <div>
                        <hr>
                        <img id="pic" src="/static/images/default_img.png" alt="上传成功后渲染" style="max-width: 196px">
                        <input type="hidden" name="pic" value="">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入备注" class="layui-textarea" autocomplete="off"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="authoritiesEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>
<!-- js部分 -->
{include file="base/_script" /}
<script>
    layui.use(['layer', 'form', 'admin', 'treeTable', 'util', 'xmSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var treeTable = layui.treeTable;
        var util = layui.util;
        var xmSelect = layui.xmSelect;
        var tbDataList = [];

        // 渲染表格
        var insTb = treeTable.render({
            elem: '#authoritiesTable',
            tree: {
                iconIndex: 0,
                idName: 'id',
                pidName: 'pid',
                isPidData: true
            },
            cellMinWidth: 100,
            cols: [
                {field: 'name', title: '分类名称',width:300},
                {field: 'pic', title: '分类图片',width:300,templet: function (d) {
                    if (d.pic){
                        return '<img src="' + d.pic + '" class="tb-img-circle" tb-img alt="'+d.name+'"/>';
                    }else{
                        return  "";
                    }
                    }},
                {field: 'id', title: 'ID'},
                {field: 'sort', title: '排序', align: 'center', width: 80},
                {title: '创建时间',align: 'center'},
                {title: '操作', templet: '#authoritiesTbBar', singleLine: false, align: 'center', width: 120}
            ],
            reqData: function (data, callback) {
                $.post("{:url('goods/goods_cate')}", function (res) {
                    tbDataList = res.data ? res.data : [];
                    callback(res.data);
                });
            }
        });

        /* 表格操作列点击事件 */
        treeTable.on('tool(authoritiesTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格搜索 */
        form.on('submit(authoritiesTbSearch)', function (data) {
            doTbSearch(data.field, 'id');
            return false;
        });

        /* 添加 */
        $('#authoritiesAddBtn').click(function () {
            showEditModel();
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            var index = layer.open({
                type: 1,
                area: '600px',
                maxmin:true,
                title: (mData ? '修改' : '添加') + '分类',
                content: $('#authoritiesEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('authoritiesEditForm', mData);
                    if (mData){
                        $("#pic").attr('src',mData.pic)
                    }
                    // 表单提交事件
                    form.on('submit(authoritiesEditSubmit)', function (data) {
                        data.field.parentId = insXmSel.getValue('valueStr');
                        var loadIndex = layer.load(2);
                        $.post("{:url('goods/goods_cate_save')}", data.field, function (res) {
                            layer.close(loadIndex);
                            if (res.code == 0) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.refresh();

                                updateSideMenu()

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'json');
                        return false;
                    });

                    // 渲染下拉树
                    var insXmSel = xmSelect.render({
                        el: '#authoritiesEditParentSel',
                        height: '250px',
                        name:"pid",
                        data: insTb.options.data,
                        initValue: mData ? [mData.pid] : [],
                        model: {label: {type: 'text'}},
                        prop: {
                            name: 'name',
                            value: 'id'
                        },
                        radio: true,
                        clickClose: true,
                        tree: {
                            show: true,
                            indent: 15,
                            strict: false,
                            expandedKeys: true
                        },
                        filterable: true,
                    });
                    // 弹窗不出现滚动条
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                }
            });
            layer.full(index)
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.name + '”权限吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post("{:url('goods/goods_cate_del')}", {id: obj.data.id}, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 0) {
                        layer.msg(res.msg, {icon: 1});
                        updateSideMenu()
                        insTb.refresh();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'json');
            });
        }

        /* 搜索表格数据 */
        function doTbSearch(field, idName) {
            var ids = [], isClear = true;
            for (var i = 0; i < tbDataList.length; i++) {
                var item = tbDataList[i], flag = true;
                for (var f in field) {
                    if (!field.hasOwnProperty(f)) continue;
                    if (!field[f]) continue;
                    isClear = false;
                    if (!item[f] || item[f].indexOf(field[f]) === -1) {
                        flag = false;
                        break;
                    }
                }
                if (flag) ids.push(item[idName]);
            }
            if (isClear) {
                insTb.clearFilter();
            } else {
                insTb.filterData(ids);
            }
        }

        /* 点击图片放大 */
        $(document).off('click.tbImg').on('click.tbImg', '[tb-img]', function () {
            layer.photos({photos: {data: [{src: $(this).attr('src'),alt:$(this).attr("alt")}]}, shade: .1, closeBtn: true});
        });

    });
</script>
</body>
</html>
